 /**
 * 
 */
package org.wmine.visualization.eventhandlers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;

import javax.swing.BorderFactory;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.xml.parsers.ParserConfigurationException;

import org.wmine.core.MainWindow;
import org.wmine.core.ProjectManager;
import org.wmine.visualization.DrawFrequencyDistributionGraph;
import org.wmine.visualization.DrawKmeansClusteringGraph;
import org.wmine.visualization.DrawLinearRegressionGraph;
import org.wmine.visualization.DrawScatterPlot;
import org.wmine.visualization.IDrawMiningAlgorithmOutput;
import org.wmine.visualization.SetChartPanel;
import org.wmine.visualization.data.AbstractChartDataProvider;
import org.wmine.visualization.data.FrequencyDistributionData;
import org.wmine.visualization.data.FrequencyDistributionDataProvider;
import org.wmine.visualization.data.KMeansData;
import org.wmine.visualization.data.KMeansDataProvider;
import org.wmine.visualization.data.FrequencyDistributionData.FreqDistDataSingleClass;
 

import org.xml.sax.SAXException;

import sun.java2d.SunGraphicsEnvironment.TTFilter;

/**
 * @author Rajeev Kumar Thakur
 *This class handles the event for Displaying Graphs
 */
public class DisplayGraphEventHandler implements ActionListener{
		private JPanel centralPanel;
		private JPanel leftPanel;
		private JPanel freqDistDescPanel;
		private JPanel linRegDescPanel;
		private JPanel kMeansDecPanel;
		private JPanel scatterPlotDecGraph;
		private static String selectedVisualizeMethod;
		private String inputAttribOne, inputAttribTwo ,sctrAttrib1, sctrAttrib2,sctrInputFileName;
		private IDrawMiningAlgorithmOutput drawMiningOutput;
		private SetChartPanel linRegGraph ;
		private SetChartPanel freqGraph;
		private SetChartPanel kMeansGraph;
		private SetChartPanel scatterPlotGraph;
		private String kMeansOpFile;
		private String frqOutputFile;
		private ArrayList<String> sctrPlotAttribList;
		private JFrame attrSelectionWindow;
	//	static String frqOutputFile = "Freq-Dist.xml";
		private static String regOutputFile = "stat-result.xml";
		
	 
	public DisplayGraphEventHandler(JPanel panel) {
			super();
			// TODO Auto-generated constructor stub
			centralPanel = panel;	
			
		}

	public void actionPerformed(ActionEvent e) {
		String temp = getSelectedComboItem();
	//    System.out.println("In actionPerformed() : " +temp);
		
	 
		 			
		 
		if (e.getActionCommand().equals("Display")){			 
				//System.out.println("in disp butt handler"+ProjectManager.getUniqueInstance().isProjectOpened());
			if(temp.equals("Frequency Dist")) {
				//System.out.println("In frequency distri");
				if(frqOutputFile == " --- Select --- ")
					JOptionPane.showMessageDialog(null, "Select the output file for FrequencyDistribution");
				else 
				{
				drawMiningOutput= new DrawFrequencyDistributionGraph(frqOutputFile);
		    	try {
		    		if(freqGraph != null){
						centralPanel.remove(freqGraph);	
					}
		    		
		    		if(linRegGraph != null){
						centralPanel.remove(linRegGraph);	
					}
		    		if(kMeansGraph != null){
						centralPanel.remove(kMeansGraph);
					}
		    		if(freqDistDescPanel != null){
						centralPanel.remove(freqDistDescPanel);
					}
		    		if(linRegDescPanel != null){
						centralPanel.remove(linRegDescPanel);
					}	
		    		if(kMeansDecPanel != null){
						centralPanel.remove(kMeansDecPanel);
					}	
				
					freqGraph =  drawMiningOutput.drawGraph();
					//freqGraph.	
					freqGraph.setVisible(true);
					freqGraph.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Graph"));
					freqGraph.setBounds(255,10,500,500);
				
					
					centralPanel.add(freqGraph);
					addFreqDistDescPanel();
				//	freqGraph.
					this.setFreqGraph(freqGraph);
					centralPanel.repaint();
					centralPanel.validate();
				} catch (ParserConfigurationException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SAXException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		    
				//centralPanel.remove(linRegGraph);
				//dp.removeAll();
				}
				
			}
			else
			if(temp.equals("Linear Regression")) {
				//System.out.println("In LInear Regression");
				
	    		//System.out.println(attribute1[0].toString());
				
				try {
					//System.out.println("in display handler "+ inputAttribOne + "  " + inputAttribTwo);
					if(inputAttribOne.equals("----Select----"))
						JOptionPane.showMessageDialog(null, "Select The attributes");
					else{
						if(freqGraph != null){
							centralPanel.remove(freqGraph);	
						}
						if(linRegGraph != null){
							centralPanel.remove(linRegGraph);
						}
						if(kMeansGraph != null){
							centralPanel.remove(kMeansGraph);
						}	
						if(freqDistDescPanel != null){
							centralPanel.remove(freqDistDescPanel);
						}
						if(linRegDescPanel != null){
							centralPanel.remove(linRegDescPanel);
						}	
						if(kMeansDecPanel != null){
							centralPanel.remove(kMeansDecPanel);
						}	
					
					drawMiningOutput = new DrawLinearRegressionGraph(regOutputFile, inputAttribOne,inputAttribTwo);
					linRegGraph  =   drawMiningOutput.drawGraph();
					linRegGraph.setVisible(true);
					linRegGraph.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Graph"));
					linRegGraph.setBounds(255,10,500,500);
						 
						centralPanel.add(linRegGraph);
						this.setLinRegGraph(linRegGraph);
						addLinRegDescPanel();
						centralPanel.repaint();
						centralPanel.validate();
						
						}
				} catch (ParserConfigurationException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (SAXException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			
				//linRegGraph.setAttributeNames(inputAttribOne, inputAttribTwo);
				
					//centralPanel.remove(freqGraph);
				//	dg.removeAll();
				
				}
			else
				if(temp.equals("KMeans Clustering")){
					if(kMeansOpFile == " --- Select --- ")
						JOptionPane.showMessageDialog(null, "Select the output file for Kmeans");
					else
					{
					AbstractChartDataProvider chartData;
					try {
						if(freqGraph != null){
							centralPanel.remove(freqGraph);	
						}
						if(linRegGraph != null){
							centralPanel.remove(linRegGraph);
						}
						if(kMeansGraph != null){
							centralPanel.remove(kMeansGraph);
						}
						if(freqDistDescPanel != null){
							centralPanel.remove(freqDistDescPanel);
						}
						if(linRegDescPanel != null){
							centralPanel.remove(linRegDescPanel);
						}	
						if(kMeansDecPanel != null){
							centralPanel.remove(kMeansDecPanel);
						}	
					
						 drawMiningOutput = new DrawKmeansClusteringGraph(kMeansOpFile);
						 kMeansGraph = drawMiningOutput.drawGraph();
						 kMeansGraph.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Graph"));
						 kMeansGraph.setBounds(255,10,500,500);
						 centralPanel.add(kMeansGraph);
						 addKmeansDescPanel();
							centralPanel.repaint();
							centralPanel.validate();
								} catch (FileNotFoundException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (ParserConfigurationException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (SAXException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}				
					}
				} 	
					 
				else
					if(temp.equals("----Select----"))	
						JOptionPane.showMessageDialog(null, "Select the appropriate Mining Algorithm");				
			} 
		else
			if(e.getActionCommand().equals("Ok")) {
				if (temp.equals("Scatter Plot")) {
				//	System.out.println("display graph event handler from btn ok "+sctrAttrib1 + " "+ sctrAttrib2+" "+ sctrInputFileName);
						drawMiningOutput = new DrawScatterPlot(sctrInputFileName, sctrAttrib1,sctrAttrib2);
						try {
							if(freqGraph != null){
								centralPanel.remove(freqGraph);	
							}
							if(linRegGraph != null){
								centralPanel.remove(linRegGraph);
							}
							if(kMeansGraph != null){
								centralPanel.remove(kMeansGraph);
							}
							if(scatterPlotGraph != null){
								centralPanel.remove(scatterPlotGraph);
							}
							if(freqDistDescPanel != null){
								centralPanel.remove(freqDistDescPanel);
							}
							if(linRegDescPanel != null){
								centralPanel.remove(linRegDescPanel);
							}	
							if(kMeansDecPanel != null){
								centralPanel.remove(kMeansDecPanel);
							}	
							if(scatterPlotDecGraph != null){
								centralPanel.remove(scatterPlotDecGraph);
							}
							
							scatterPlotGraph = drawMiningOutput.drawGraph();
							if(scatterPlotGraph != null) {
								scatterPlotGraph.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Graph"));
								scatterPlotGraph.setBounds(255,10,500,500);
								centralPanel.add(scatterPlotGraph);
								addSctrPoltDescPanel();
							}
							
								centralPanel.repaint();
								centralPanel.validate();
							
						} catch (ParserConfigurationException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (SAXException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
				}
				
				attrSelectionWindow.dispose();
			}
		}
		
	
	private void addSctrPoltDescPanel() {
		// TODO Auto-generated method stub
		
		String str = "<html>Showing the scatter plot between two selected numerical attributes </html>";
		JLabel desc = new JLabel(str);
		desc.setBounds(60,15,400,30);
		scatterPlotDecGraph = new JPanel();
		scatterPlotDecGraph.setLayout(null);
		scatterPlotDecGraph.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Description"));
		scatterPlotDecGraph.setBounds(255,510,500,60);
		scatterPlotDecGraph.add(desc);
		scatterPlotDecGraph.setVisible(true);
	 	centralPanel.add(scatterPlotDecGraph);
		centralPanel.repaint();
		centralPanel.validate();
	
	}

	private void addLinRegDescPanel() {
		// TODO Auto-generated method stub
		String str = "<html>Showing the plot between actual values and Calculated Values</html>";
		JLabel desc = new JLabel(str);
		desc.setBounds(60,15,400,30);
		linRegDescPanel = new JPanel();
		linRegDescPanel.setLayout(null);
		linRegDescPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Description"));
		linRegDescPanel.setBounds(255,510,500,60);
		linRegDescPanel.add(desc);
		linRegDescPanel.setVisible(true);
	 	centralPanel.add(linRegDescPanel);
		centralPanel.repaint();
		centralPanel.validate();
	
	}
   private void addKmeansDescPanel(){
	   String str = "<html>Showing the Clusters based on two attributes</html>";
		JLabel desc = new JLabel(str);
		desc.setBounds(60,15,400,30);
		kMeansDecPanel = new JPanel();
		kMeansDecPanel.setLayout(null);
		kMeansDecPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Description"));
		kMeansDecPanel.setBounds(255,510,500,60);
		kMeansDecPanel.add(desc);
		kMeansDecPanel.setVisible(true);
	 	centralPanel.add(kMeansDecPanel);
		centralPanel.repaint();
		centralPanel.validate();
   }
	public void addFreqDistDescPanel() {
		// TODO Auto-generated method stub
		 
		//regressionPanel.setVisible(false);
		//handler.setFreqReader(freqReader);
	
		AbstractChartDataProvider chartData;
		FrequencyDistributionData frqData;
		ArrayList<FreqDistDataSingleClass> classList;
		freqDistDescPanel = new JPanel();
		try {
			chartData = new FrequencyDistributionDataProvider(frqOutputFile);
			frqData =(FrequencyDistributionData) chartData.getChartData();
			classList = new ArrayList<FreqDistDataSingleClass>();
			classList = frqData.getClassList();
			FreqDistDataSingleClass class1 = frqData.getFreqDistDataSingleClassInstance();
			class1 = classList.get(0);
			Double d1 = class1.getLowerBound();
			Double d2 = class1.getUpperBound();
			Double d = d2-d1;
			JLabel freqDistDetLabel = new JLabel("Parameters Frequency Distribution");
			freqDistDetLabel.setBounds(10,15,200,30);
			
			JLabel intervalLabel = new JLabel("Interval :"+d.toString());
			intervalLabel.setBounds(230,15,150,30);
			JLabel startingLabel = new JLabel("Starting at :"+d1.toString());
			startingLabel.setBounds(380,15,150,30);
			freqDistDescPanel.setLayout(null);
			freqDistDescPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(),"Description"));
			freqDistDescPanel.setBounds(255,510,500,60);
			freqDistDescPanel.add(freqDistDetLabel);
			freqDistDescPanel.add(intervalLabel);
			freqDistDescPanel.add(startingLabel);
			freqDistDescPanel.setVisible(true);
		 	centralPanel.add(freqDistDescPanel);
			centralPanel.repaint();
			centralPanel.validate();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		};
		
	}
	
	public static String getSelectedComboItem() {
		return selectedVisualizeMethod;
	}

	public void setSelectedComboItem(String selectedComboItem) {
		DisplayGraphEventHandler.selectedVisualizeMethod = selectedComboItem;
		//System.out.println(selectedComboItem);
	}
	public void setLinearRegInputAttribs(String str1, String str2){
		inputAttribOne = str1;
		inputAttribTwo = str2;
	}

	public SetChartPanel getFreqGraph() {
		if(freqGraph != null)
		return freqGraph;
		else
		return null;
	}

	public void setFreqGraph(SetChartPanel freqGraph) {
		this.freqGraph = freqGraph;
	}

	public SetChartPanel getLinRegGraph() {
		if(linRegGraph != null)
			return linRegGraph;
		else
			return null;
	}

	public void setLinRegGraph(SetChartPanel linRegGraph) {
		this.linRegGraph = linRegGraph;
	}

	public void setKmeansOpFile(String opFile) {
		// TODO Auto-generated method stub
		kMeansOpFile = opFile;
	//	System.out.println("in display graph event handler "+kMeansOpFile);
	}

	public void setFrqOutputFile(String string) {
		// TODO Auto-generated method stub
		frqOutputFile = string;
	}

	 
	public void setAttribSelectionWindow(JFrame attribSelectionWindow) {
		// TODO Auto-generated method stub
		attrSelectionWindow = new JFrame();
		attrSelectionWindow = attribSelectionWindow;
	}
 
	public void setScatterPlotAttribute1(String attribute1) {
		// TODO Auto-generated method stub
		sctrAttrib1 = attribute1 ; 
	//	 System.out.println("in Display graph event handler x attrib "+sctrAttrib1);
			
	}

	public void setScatterPlotAttribute2(String attribute2) {
		// TODO Auto-generated method stub
		sctrAttrib2 = attribute2 ;
	//	 System.out.println("in Display graph event handler y attrib "+sctrAttrib2);
			
	}

	public void setInputScatrPlotFileName(String inputFileName) {
		// TODO Auto-generated method stub
		sctrInputFileName = new String();
		sctrInputFileName = inputFileName;
	}

	 
}
